<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言板</title>

    <style>
        body {
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background-image: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
        }

        .container {
            width: 100%;
            max-width: 400px;
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            animation: fadeIn 0.5s ease-in-out;
        }

        .header {
            background-color: #ff6b6b;
            color: rgb(248, 248, 248);
            padding: 0.5rem;
            text-align: center;
        }

        .conntent {
            margin-left: 4.5rem;
            margin: 0, auto;
        }

        .conntent #btn {
            width: 250px;
            height: 40px;
            color: white;
            background-color: rgb(255, 12, 36);
            border-radius: 5px;
            border: none;
            margin-top: 1rem;
            margin-bottom: 1rem;
        }
    </style>
</head>

<body>

    <div class="container">
        <div class="header">
            <h1>留言板</h1>
        </div>

        <div class="conntent">
            你的名字：<input type="text" id="1"><br>
            你对谁说：<input type="text" id="2"><br>
            说什么话：<input type="text" id="3"><br>
            <button id="btn" onclick="submit()">确认提交</button>
        </div>
    </div>


    <script src="./jquery-3.7.1.min.js"></script>
    <script>

        //下面可以放到方法里面，但是要记得调用
        getList();
        function getList() {
            $.ajax({
                type: "get",
                url: "/info/getInfo",
                success: function (messages) {
                    for (let msg of messages) {
                        let html = "<div>" + msg.from_who + " 对 " + msg.to_who + " 说: " + msg.messages + "</div>";

                        //添加到容器container中
                        $(".container").append(html);
                    }
                }
            });
        }


        //点击按钮响应该函数
        function submit() {
            //从文本框中获取信息
            let from_who = $("#1").val();
            let to_who = $("#2").val();
            let info = $("#3").val();

            //检查是否为空
            if (from_who == "" || to_who == "" || info == "") {
                alert("请检查输入内容是否为空！");
                return;
            }

            //不为空则，发起请求
            $.ajax({
                url: "/info/publish",
                contentType: "application/json",
                type: "post",
                data: JSON.stringify({
                    from_who: from_who,
                    to_who: to_who,
                    messages: info
                }),
                //成功则回调
                success: function (ret) {
                    if (ret.ok == 1) {
                        alert("添加成功！");
                        let html = "<div>" + from_who + " 对 " + to_who + " 说: " + info + "</div>";

                        //添加到容器container中
                        $(".container").append(html);
                        $(":text").val("");
                    } else {
                        alert("添加失败！");
                    }
                }

            })
        }


    </script>


</body>

</html>